package com.black.utils.mail;

import com.black.utils.config.loader.PropertiesLoader;
import com.black.utils.encrypt.sm4.SM4Util;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.util.Properties;

/**
 * @author ylx
 */
public class MailProperties {
    public static final String QQ = "qq";
    public static final String WY = "163";
    @Getter
    public Properties mailProp = new Properties() {
        {
            setProperty("mail.smtp.auth", "true");
            setProperty("mail.smtp.protocol", "smtp");
            setProperty("mail.smtp.ssl.enable", "true");
            setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }
    };
    private final Config config;

    public MailProperties(String source) {
        try {
            config = PropertiesLoader.loadConfig(source, Config.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String getHost() {
        return config.host;
    }

    public int getPort() {
        return config.port;
    }

    public String getUsername() {
        return config.encrypt ? SM4Util.decryptData_CBC(config.username) : config.username;
    }

    public String getPassword() {
        return config.encrypt ? SM4Util.decryptData_CBC(config.password) : config.password;
    }

    @Getter
    @Setter
    public static class Config {
        private String host;
        private int port;
        private String username;
        private String password;
        private boolean encrypt;
    }

    public static class Builder {
        public static MailProperties build(String type) {
            return new MailProperties("/email_" + type + ".properties");
        }
    }
}
